package org.domeos.framework.api.mapper.domeos.storage;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.domeos.framework.api.model.storage.Storage;

public interface StorageMapper {
	
	@Insert("INSERT INTO storage(name, description, createTime, removeTime, removed, type, path, server, clusterId, usedPod, data) values ("
			+ "#{item.name}, #{item.description}, #{item.createTime}, #{item.removeTime}, #{item.removed}, "
			+ "#{item.type}, #{item.path}, #{item.server}, #{item.clusterId}, #{item.usedPod}, #{data})")
	@Options(useGeneratedKeys = true, keyProperty = "item.id", keyColumn = "id")
	int insertStorage(@Param("item") Storage item, @Param("data") String data);
	
	@Update("UPDATE storage SET name=#{item.name}, description=#{item.description}, type=#{item.type}, path=#{item.path}, "
			+ "server=#{item.server}, clusterId=#{item.clusterId}, data=#{data} WHERE id=#{item.id}")
	void updateStorage(@Param("item") Storage item, @Param("data") String data);
	
	@Select("select * from storage where clusterId=#{clusterId} and removed=0")
	List<Storage> listStorageByClusterId(@Param("clusterId") int clusterId);
	
	@Select("select * from storage where path=#{path} and server=#{server} and removed=0")
	List<Storage> listStorageByPathAndServer(@Param("path") String path, @Param("server") String server);
	
	@Select("select * from storage where id=#{id} and removed=0")
	Storage getStorageById(@Param("id") int id);
	
	@Select("select * from storage where name=#{name} and removed=0 limit 1")
	Storage getStorageByName(@Param("name") String name);
}
